package com.koron.css2.newProjectCenter.function.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.koron.css2.ServerInterface;
import com.koron.css2.newProjectCenter.function.base.BaseQueryAction;
import com.koron.css2.newProjectCenter.function.bean.AccessoryLogBean;
import com.koron.css2.newProjectCenter.function.mapper.AccessoryLogMapper;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.css2.serviceManage.utils.JsonUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.koron.ebs.mybatis.EnvSource;
import org.koron.ebs.mybatis.SessionFactory;
import org.swan.bean.MessageBean;

import java.util.List;

@SuppressWarnings("rawtypes")
public class AccessoryLogController implements ServerInterface {
    private static Logger log = LogManager.getLogger(AccessoryLogController.class);
    @Override
    public MessageBean<?> exec(SessionFactory factory, UserInfoBean userInfo, RequestBean req) {
        try {
            BaseQueryAction action = JsonUtils.objectToPojo(req.getData(), BaseQueryAction.class);
            AccessoryLogMapper mapper = factory.getMapper(AccessoryLogMapper.class, EnvSource.DEFAULT);
            PageHelper.startPage(action.getPage(), action.getPageCount());
            List<AccessoryLogBean> logs = mapper.selectAll();
            PageInfo pageInfo = new PageInfo<>(logs);
            MessageBean<PageInfo> infoMessageBean = MessageBean.create(0, "success", PageInfo.class);
            infoMessageBean.setData(pageInfo);
            return infoMessageBean;
        } catch (Exception e) {
            log.error(e);
            return MessageBean.create(1, "查询失败", null);
        }
    }
}
